package ua.epam.diff;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageComparator {

    public static File compare(String pathToOrigin, String pathToCopy) {


        BufferedImage originImg = null;
        BufferedImage copyImg = null;

        try {
            originImg = ImageIO.read(new File(pathToOrigin));
            copyImg = ImageIO.read(new File(pathToCopy));
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedImage diffImg = new BufferedImage(originImg.getWidth(), originImg.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = diffImg.createGraphics();


        g.setBackground(Color.BLACK);
        g.setColor(Color.YELLOW);

//               g.drawImage(hittedEmptyImg, imageSize * (i + 1), imageSize * (j + 1), null);
        //String fileName = "src/main/resources/gif/temp/" + System.currentTimeMillis();

        for (int x = 0; x < originImg.getWidth(); ++x) {
            for (int y = 0; y < originImg.getHeight(); ++y) {
                int oldARGB = originImg.getRGB(x, y);
                int newARGB = copyImg.getRGB(x, y);
                if (oldARGB != newARGB) {
                    g.drawLine(x,y,x,y);
                }
            }

        }

        String fileName = "png/diffImg"+System.currentTimeMillis();

        File file = null;
        try {
            file = new File(fileName + "." + "png");
            ImageIO.write(diffImg, "png", file);  // ignore returned boolean
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}
